package service;

import com.google.gson.Gson;
import model.Response;
import util.HttpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

//实现业务服务接口
public class QkyRobotServiceImpl implements RobotService {


    //青云客api
    private static final String apiTpl = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s";


    private static  final Gson gson = new Gson();



    @Override
    public Response qa(String msg) {

        String api = null;
        try {
            api = String.format(apiTpl, URLEncoder.encode(msg,"UTF-8") );//拼接请求的url
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String result =  HttpUtils.request(api);//通过Http工具类来发送请求

        //可以做逻辑判断，比如null的时候，或者出错

        Response response = gson.fromJson(result,Response.class);//将json字符串转化为Response对象

        return response;
    }


}
